﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace TK.Common.AspNetCore
{
    /// <summary>
    /// 统一结果
    /// </summary>
    public class UnaryResult
    {
        /// <summary>
        /// 200成功，其他异常
        /// </summary>
        public bool IsSuccess => StatusCode== 200;

        /// <summary>
        /// 200成功，其他异常
        /// </summary>
        public int StatusCode { get; set; }

        /// <summary>
        /// 其他消息或异常消息
        /// </summary>
        public string Message { get; set; }

        #region 快捷方法
        /// <summary>
        /// ok
        /// </summary>
        public static UnaryResult OK(string msg = "成功")
        {
            return new UnaryResult { StatusCode = (int)HttpStatusCode.OK, Message = msg };
        }

        /// <summary>
        /// ok
        /// </summary>
        public static UnaryResult<TData> OK<TData>(TData data, string msg = "成功")
        {
            return new UnaryResult<TData> { StatusCode = (int)HttpStatusCode.OK, Message = msg, Data = data };
        }

        /// <summary>
        /// fail
        /// </summary>>
        public static UnaryResult Fail(string msg = "操作失败")
        {
            return new UnaryResult { StatusCode = (int)HttpStatusCode.ExpectationFailed, Message = msg };
        }

        /// <summary>
        /// fail
        /// </summary>>
        public static UnaryResult<TData> Fail<TData>(TData data, string msg = "操作失败")
        {
            return new UnaryResult<TData> { StatusCode = (int)HttpStatusCode.ExpectationFailed, Message = msg, Data = data };
        }

        /// <summary>
        /// 自定义结果;200成功，其他异常
        /// </summary>
        public static UnaryResult Create(bool isSuccess, int statusCode, string message)
        {
            return new UnaryResult { StatusCode= statusCode, Message = message };
        }
        #endregion

    }

    /// <summary>
    /// 带数据的统一结果
    /// </summary>
    public class UnaryResult<TData> : UnaryResult
    {
        /// <summary>
        /// playload数据载体
        /// </summary>
        public TData Data { get; set;}
    }
}